/*
 *     Prionace - A file navigator to manage your files
 *
 *     Copyright (C) 2012 Francivan Bezerra
 *
 *     This file is part of Prionace.
 *
 *     Prionace is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Prionace is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Prionace.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fba.prionace.process;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ExecChooser extends JPanel implements ActionListener {

	private JLabel mimeType;

	private JTextField command;

	private JButton browse;

	public ExecChooser() {
		init();
	}

	private void init() {
		mimeType = new JLabel();
		command = new JTextField();
		browse = new JButton("...");
		browse.addActionListener(this);
		setLayout(new BorderLayout());
		add(command, BorderLayout.CENTER);
		add(browse, BorderLayout.EAST);
		add(mimeType, BorderLayout.SOUTH);
	}

	public String getMimeType() {
		return mimeType.getText();
	}

	public void setMimeType(String mimeType) {
		StringBuffer showMimeType = new StringBuffer("<html>");
		showMimeType.append("<b>Mime type: </b><i>");
		showMimeType.append(mimeType);
		showMimeType.append("</i></html>");
		this.mimeType.setText(showMimeType.toString());
	}

	public String getCommand() {
		return command.getText();
	}

	public void setCommand(String command) {
		this.command.setText(command);
	}

	public void actionPerformed(ActionEvent e) {
		JFileChooser chooser = new JFileChooser();
		ExecFileFilter filter = new ExecFileFilter();

		chooser.setFileFilter(filter);
		chooser.showOpenDialog(this);

		if (chooser.getSelectedFile() != null) {
			command.setText(chooser.getSelectedFile().getAbsolutePath());
		}
	}
}
